package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.mapper.EmployeeMapper;
import cn.wolfcode.quest.EmployeeQueryObject;
import cn.wolfcode.quest.QueryObject;
import cn.wolfcode.result.PageResult;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;

@Service
public class EmployeeServiceImpl implements IEmployeeService {
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void deleteById(Long id) {
        employeeMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Employee selectById(Long id) {
        return employeeMapper.selectById(id);
    }

    @Override
    public void insert(Employee emp) {
        employeeMapper.insert(emp);
    }

    @Override
    public Employee get(Long id) {
        Employee employee = employeeMapper.selectByPrimaryKey(id);
        return employee;
    }

    @Override
    public List<Employee> selectAll() {
        List<Employee> employees = employeeMapper.selectAll();
        return employees;
    }

    @Override
    public void update(Employee emp) {
        employeeMapper.updateByPrimaryKey(emp);
    }

    @Override
    public void insertRole(Employee emp, Long[] ids) {
        //添加员工的数据
        emp.setPassword(new Md5Hash(emp.getPassword(),emp.getName()).toString());
        employeeMapper.insert(emp);
        //直接添加数据
        if (ids!=null){
            employeeMapper.insertRole(emp, ids);
        }
    }

    @Override
    public void updateRole(Employee emp,Long[] ids) {
        System.out.println(ids);
        //修改员工表的数据
        employeeMapper.updateByPrimaryKey(emp);

        //先删除关系表中的数据
        employeeMapper.deleteRole(emp);
        //再增加关系表中的数据
        if (ids!=null){
            employeeMapper.insertRole(emp, ids);
        }
    }

    @Override
    public PageInfo<Employee> query(EmployeeQueryObject qo) {
        //查询符合条件的所有条数
        /*int i = employeeMapper.selectForCount(qo);
        //如果条数为0,则不用查询具体条数
        if (i==0){
            PageResult<Employee> result = new PageResult<>(1, qo.getPageSize(), 0, Collections.emptyList());
            return result;
        }
        //条数不为0查询所有数据
        List<Employee> employees = employeeMapper.selectForList(qo);
        if (employees.size()<=0&&qo.getCurrentPage()>1){
            qo.setCurrentPage(qo.getCurrentPage()-1);
            employees = employeeMapper.selectForList(qo);
        }
        //装进result中*/
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Employee> employees = employeeMapper.selectForList(qo);

        return new PageInfo<>(employees);
    }

    @Override
    public void updatePwd(Long id, String newPassword) {
        employeeMapper.updatePwd(id, newPassword);
    }


    //员工的批量删除的操作
    @Override
    public void batchDelect(Long[] ids) {
           employeeMapper.delectBatch(ids);
    }
     /*
           通过用户名查找对象
      */
    @Override
    public Employee QueryByName(String name) {
        return employeeMapper.SelectEmployeeByName(name);
    }
    /*
          文件的导出的功能
     */
    @Override
    public Workbook exportXls() {
        //创建excal文件
        Workbook workbook = new HSSFWorkbook();
         //创建sheet
        Sheet sheet = workbook.createSheet("员工名单");
        //创建行
        Row row = sheet.createRow(0);
      //创建单元格
        row.createCell(0).setCellValue("姓名");
        row.createCell(1).setCellValue("邮箱");
        row.createCell(2).setCellValue("年龄");
        List<Employee> employees = employeeMapper.selectAll();

      for (int i=0;i<employees.size();i++){
          Row row1 = sheet.createRow(i + 1);
          Employee employee = employees.get(i);
          row1.createCell(0).setCellValue(employee.getName());
          row1.createCell(1).setCellValue(employee.getEmail());
          row1.createCell(2).setCellValue(employee.getAge());
      }

        return workbook;
    }

    @Override
    public void importXls(MultipartFile file) throws IOException {

         Workbook workbook= new HSSFWorkbook(file.getInputStream());
         Sheet sheet = workbook.getSheetAt(0);
        int lastRowNum = sheet.getLastRowNum();
        for (int i=1;i<=lastRowNum;i++){
            Row row = sheet.getRow(i);
            String name = row.getCell(0).getStringCellValue();
            //判断如果用户名是空，就不再往下读
            if(!StringUtils.hasLength(name.trim())){
                return;
            }
            Employee employee = new Employee();
            employee.setName(name);
            employee.setEmail(row.getCell(1).getStringCellValue());
            //获取文本格式的单元格内容
            double v = row.getCell(2).getNumericCellValue();

                employee.setAge((int)v);


            //获取数值类型的单元格内容
            //double value = row.getCell(2).getNumericCellValue();
            //employee.setAge((int)value);
            //设置默认密码1
            employee.setPassword("1");
            //调用保存方法
          employeeMapper.insert(employee);
        }

    }

    /*
     更改用户的状态,禁用账号
 */
    @Override
    public void updataStatus(Long id, boolean status) {
          employeeMapper.updataStatus(id,status);

    }

    @Override
    public List<Employee> selectByIds(Set<Long> ids) {
        ArrayList<Employee> list = new ArrayList<>();
        for (Long id : ids) {
            Employee employee = employeeMapper.selectByPrimaryKey(id);
            list.add(employee);
        }


        return list;
    }
    //销售人员的查询下拉框,只查询有Market角色或者Market_Manager角色的员工
    @Override
    public List<Employee> selRolesBySn(String... sn) {
        return employeeMapper.selRolesBySn(sn);
    }

}
